package com.vans.practice.executors;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * @Description: 创建线程池的正确方式： 自己定义TheadPoolExecutor； 使用guava提供的ThreadFactoryBuilder来创建线程池
 * @Note:
 * @Author elle
 * @Date 2020/10/4 12:20
 */
public class CreateTheadPoolExecutorByRightWay {


    private static ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("pool-%d").build();
    private static ExecutorService executorService = new ThreadPoolExecutor(2, 200, 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE), threadFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            Thread.sleep(10000);
            System.out.println(i);
            executorService.execute(new SubTask());
        }
    }
}

